#include "MMOpenMarketOperatorSettingDlg.h"
#include "../FIDataCenter.h"
#include <qbfixedincome/StringResource.h>
#include <QVBoxLayout>
#include <QLabel>
#include <QFrame>
#include <QPushButton>
#include <list>
#include <set>
#include <core/StyleMgr.h>


MMOpenMarketOperatorSettingDlg::MMOpenMarketOperatorSettingDlg(QWidget* parent)
	:SPopupWindow(parent)
{
	setFixedSize(444, 326);
	setTitle(tr("公开市场操作：净投放/净回笼计算设置"));

	QVBoxLayout* mainLayout = contentLayout();
	mainLayout->setSpacing(5);

	QLabel* promptLabel = new QLabel;
	promptLabel->setFixedSize(102, 24);
	promptLabel->setStyleSheet(QString("QLabel{background-color:#%1;}")
		.arg(NAME_COLOR("PartPlate").rgb(), 0, 16));
	promptLabel->setText(NewStringTable::strCapitalMarketOMOSetting(8));

	mainLayout->addWidget(promptLabel, 0, Qt::AlignLeft);

	std::list<QBAPubOptNetInvFactor> listContent = FIDataCenter::instance().m_dataOMO.m_lstInv;
	std::set<int> setContent = FIDataCenter::instance().m_dataOMO.m_setInvSetting;
	std::map<int, QBAPubOptNetInvFactor> mapContent;
	for (auto itor :listContent)
	{
		mapContent.insert(std::make_pair(itor.order, itor));
	}

	m_vecCheck.clear();
	for (auto itor : mapContent)
	{
		SCheckEx* check = new SCheckEx(tr(itor.second.name.c_str(), itor.second.name_en.c_str()), this);
		check->setSelectType(SCheckEx::SelectType::Check);
		check->setIndex(itor.second.id);
		bool bCheck = (setContent.find(itor.second.id)!=setContent.end());
		if (bCheck)
			check->setSelectState(Qt::Checked);

		m_vecCheck.push_back(check);
		mainLayout->addWidget(check);
	}

	QPushButton* btn = addBtn(QMessageBox::Ok, tr("确定"));
	connect(btn, &QPushButton::pressed, this, &MMOpenMarketOperatorSettingDlg::applyChange);

	btn = addBtn(QMessageBox::Cancel, tr("取消"));
	connect(btn, &QPushButton::pressed, this, QOverload<>::of(&QDialog::reject));

	resize(sizeHint());
}

void MMOpenMarketOperatorSettingDlg::applyChange()
{
	FIDataCenter::instance().m_dataOMO.m_setInvSetting.clear();
	for (auto itor : m_vecCheck)
	{
		if (itor->isSelected())
			FIDataCenter::instance().m_dataOMO.m_setInvSetting.insert(itor->index());
	}

	emit onStateChanged();

	FIDataCenter::instance().reqOMOSaveSetting();

	accept();
}